﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ROLRouter.Base;
using ROL.Core.Utils;
using System.Threading;
using System.Net;
using System.IO;

namespace ROLRouter
{
    class Program
    {
        static Server _server { get; set; }
        static Thread listener { get; set; }
        static bool IsRunning;

        public static ManualResetEvent clientConnected =
               new ManualResetEvent(false);

        static void Main(string[] args)
        {
            Console.WriteLine(ReadContentsOfFile("router.txt"));

            StringBuilder info = new StringBuilder();
            info.AppendLine("################################################################################");
            info.AppendLine("#####             License: Microsoft Public License (Ms-PL)                #####");
            info.AppendLine("#####     Website: http://realmserveroffline.codeplex.com/license          #####");
            info.AppendLine("#####                Copyright 2012 ROL Dev Team                           #####");
            info.AppendLine("################################################################################");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine(info.ToString());
            Console.ResetColor();
                Commands();
            
            
        }
        public static void Commands()
        {
            Console.WriteLine(ReadContentsOfFile("cmds.txt"));
            string input = null;
            while (true)
            {
                Console.Write("\nROL Router >> ");
                input = Console.ReadLine();
                switch (input.ToLower())
                {
                    case "start":
                        {
                            if (IsRunning)
                            {
                                Console.WriteLine("Server is already accepting connections.");
                            }
                            else
                            try
                            {
                               
                                    _server = new Server();
                                    listener = new Thread(new ThreadStart(_server.Start));
                                    listener.IsBackground = true;
                                    listener.Start();
                                    IsRunning = true;
                                }
                            
                            catch
                            {
                                Console.WriteLine("Router is already running.");
                            }
                        }

                        break;
                    case "stop":
                        {
                            if (!IsRunning) { Console.WriteLine("Server is not running."); }
                            else
                            {
                                _server._shouldStop = true;
                                _server.listener.Stop();
                                Console.WriteLine("ROL Router has stopped accepting connections.");
                                IsRunning = false;
                            }
      
                        }
                        break;
                    case "exit":
                        {
                            Environment.Exit(0);
                        }
                        break;
                    case "running":
                        if (IsRunning)
                        {
                            Console.WriteLine("Server is running.");
                        }
                        else
                            Console.WriteLine("Server is not running.");
                        break;
                    default:
                        Console.WriteLine("In command block");
                        break;
                }
            }
        }

        static string ReadContentsOfFile(string file)
        {
            StreamReader fr = new StreamReader(file);
            string strRead = fr.ReadToEnd();

            fr.Close();
            return strRead;
        }
    }
}
